<?php
/*
 * 管理员
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Session;
use think\facade\Db;
use app\common\model\Admin as Guanli;
use app\common\model\AuthGroupAccess;

class Admin extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function lists()
    {
        $pwd = config("app.default_pwd");//默认密码
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){$query->where('user_name|mobile_phone','LIKE', '%'.trim($param['title']).'%');};
        }else{
            $where = 1;
        }
        $list = Guanli::getAdminList($where);
        return $this->fetch('index', ['pwd' => $pwd,'list' => $list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'admin');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            empty($param['plain_code']) && $param['plain_code'] = config("app.default_pwd");
            $pwd = morse_code($param['plain_code']);
            $param['password'] = $pwd['password'];//密码
            $param['salt'] = $pwd['salt'];//盐
            $result = Guanli::create($param);
            !$result && $this->error($this->errorMsg);
            AuthGroupAccess::create(['uid' => $result->id, 'group_id' => $param['group_id']]);
            insert_admin_log('添加了管理员');
            $this->success('添加成功');
        }
        return $this->fetch('save', ['authGroup' => Db::table('auth_group')->where('status', 1)->select()]);
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param  = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'admin');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //更新数据
            $result = Guanli::update($param);
            !$result && $this->error($this->errorMsg);
            $verify && AuthGroupAccess::update(['group_id' => $param['group_id']], ['uid' => $param['id']]);
            insert_admin_log('修改了管理员');
            $this->success('修改成功');
        }
        return $this->fetch('save', [
            'data'      => Guanli::with('authGroupAccess')->find(input('id')),
            'authGroup' => Db::table('auth_group')->where('status', 1)->select(),
        ]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['id'] === "1" && $this->error('数据未能解析，无法删除数据');
            Guanli::destroy($param['id']);
            AuthGroupAccess::where('uid', $param['id'])->delete();
            insert_admin_log('删除了管理员');
            $this->success('删除成功');
        }
    }
    //重置密码
    public function reset()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['plain_code'] = config("app.default_pwd");
            $pwd = morse_code($param['plain_code']);
            $param['password'] = $pwd['password'];//密码
            $param['salt'] = $pwd['salt'];//盐
            $result = Guanli::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('重置了用户'.$param['id'].'的密码');
            $this->success('重置成功');
        }
    }
    public function log()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $name = trim($param['title']);
            $where = function ($query) use ($name){$query->where('user_name','LIKE', '%'.$name.'%');};
        }else{
            $where = 1;
        }
        $list = Db::table('admin_log')->where($where)->order('create_time desc')->paginate();
        return $this->fetch('log',['list'=>$list]);
    }

    public function truncate()
    {
        if ($this->request->isPost()) {
            // Guanli::query('TRUNCATE ' . config('database.prefix') . 'admin_log');
            // $this->success('操作成功');
        }
    }









}
